package com.oms.module.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.oms.common.Page;
import com.oms.common.PageableVO;
import com.oms.common.ResponseDTO;
import com.oms.module.admin.constant.AdminResponseCodeConstant;
import com.oms.module.admin.constant.AnnouncementOrderEnum;
import com.oms.module.admin.domain.entity.AnnouncementEntity;
import com.oms.module.admin.domain.vo.AnnouncementVO;
import com.oms.module.admin.service.AnnouncementService;
import com.oms.util.OrderEncapsulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author zhanghailong QQ:38592077
 * @Description TODO
 * @Date 15:40 2020/3/26
 */
@RestController
@Api(value = "管理端广告管理")
@RequestMapping("/admin/announcementManage")
public class AnnouncementManageController {

    @Resource
    AnnouncementService announcementService;

    @ApiOperation( value = "商家公告列表 @author Zhang Hailong")
    @PostMapping("/announcementList")
    public ResponseDTO<Page<AnnouncementEntity>> announcementList(@RequestBody @Validated PageableVO<AnnouncementOrderEnum> pageableVO){
        Page<AnnouncementEntity> result = new Page<>();
        BeanUtils.copyProperties(pageableVO, result );
        QueryWrapper<AnnouncementEntity> query = Wrappers.query();
        OrderEncapsulation.order( pageableVO, query );
        result = announcementService.page(  result,query);
        return ResponseDTO.succData(result);
    }


    @ApiOperation( value = "添加公告 @author Zhang Hailong")
    @PostMapping("/addAnnouncement")
    public ResponseDTO<Page<AnnouncementEntity>> addAnnouncement(@RequestBody @Validated AnnouncementVO announcementVO){
        AnnouncementEntity announcementEntity = new AnnouncementEntity();
        BeanUtils.copyProperties( announcementVO, announcementEntity);
        boolean save = announcementService.save( announcementEntity );
        if (!save) {
            return ResponseDTO.wrap( AdminResponseCodeConstant.ADD_ANNOUNCEMENT_ERROR );
        }
        return ResponseDTO.succ();
    }
}
